// @algorithm @lc id=1000021 lang=cpp 
// @title smallest-k-lcci


// #define print(...)
// @test([1,3,5,7,2,4,6,8],4)=[1,2,3,4]
class Solution {
public:
    vector<int> smallestK(vector<int>& arr, int k) {
        vector<int> ans;
        if(!k) return ans;
        priority_queue<int> q; // 大顶堆
        int i=0;
        while(i<k)
            q.push(arr[i++]);
        while(i<arr.size()){
            if(q.top() > arr[i]){
                q.pop();
                q.push(arr[i]);
            }
            i++;
        }
        while(!q.empty())
            ans.push_back(q.top()), q.pop();
        return ans;
    }
};